package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.UserLike;
import com.tanhua.service.UserLikeApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author げっしょく
 * @date 2021/10/31
 */
@DubboService
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean b) {
        try {
            //判断操作人和被操作人之间以前是否有数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);

            if (userLike != null) {
                //有:修改数据
                Update update = new Update().set("isLike", b).set("updated", System.currentTimeMillis());
                mongoTemplate.updateFirst(query, update, UserLike.class);
            } else {
                //没有:新增
                userLike = new UserLike();
                userLike.setIsLike(b);
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                mongoTemplate.save(userLike);

            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    //找出我对访问用户是否喜欢
    @Override
    public Map<Long, UserLike> getLikes(List<Long> visitorUserId, Long userId) {
        //设置条件
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("likeUserId").in(visitorUserId).and("isLike").is(true);
        Query query = Query.query(criteria);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        Map<Long, UserLike> collect = userLikes.stream()
                .collect(Collectors.toMap(userLike -> userLike.getLikeUserId(), Function.identity()));
        return collect;
    }

    //通过数据库查找喜欢我的有多少人
    @Override
    public List<UserLike> getLikeMe(Long userId) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return userLikes;
    }

    //查询我喜欢的用户有多少喜欢我的
    @Override
    public List<UserLike> getWhoLoveMe(List<Long> userIds, Long id) {
        //设置条件
        Criteria criteria = Criteria.where("userId").in(userIds).and("likeUserId").is(id).and("isLike").is(true);
        Query query = Query.query(criteria);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return userLikes;
    }

<<<<<<< HEAD
    @Override
    public Boolean fansLike(Long uid, Long id) {
        Query query = Query.query(Criteria.where("userId").is(uid).and("likeUserId").is(id));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        if (CollUtil.isEmpty(userLikes)) {
            UserLike userLike = new UserLike();
            userLike.setIsLike(true);
            userLike.setLikeUserId(id);
            userLike.setUserId(uid);
            userLike.setUpdated(System.currentTimeMillis());
            userLike.setCreated(System.currentTimeMillis());
            mongoTemplate.save(userLike);
            return true;
        } else {
            return false;
        }
    }


=======
    /**
     * 取消喜欢
     * @param uid
     * @param id
     * @return
     */
    @Override
    public boolean deleteLikeUser(Long uid, Long id) {
        //构建criteria
        try {
            Query query = Query.query(Criteria.where("userId").is(id).and("likeUserId").is(uid).and("isLike").is(true));
            mongoTemplate.remove(query,UserLike.class);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
>>>>>>> 07fdc973c42575f9b894fb80ea1771a416980360
}
